package Dialogos;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.WindowConstants;
import javax.swing.border.LineBorder;
import javax.swing.border.TitledBorder;

@SuppressWarnings("serial")
public class DialogoCambiarApariencia extends JDialog implements ActionListener{

	/**
	 * Variables de Instancia
	 */
	//Variables para el funcionamiento
	private boolean bomba, tablero;//(VARIABLES DE APOYO)bomba true = bomba1_32.png, bomba false = bommba2_32.png - tablero true...(32 indica los pixeles)
	private Icon iBomba1, iBomba2, iTablero1, iTablero2;// de estos iconos seleccionaremos una bomba y un tablero para pasarlos a iBombaFinal y iTableroFinal como definitivos
	private Icon iBombaFinal, iTableroFinal;// iconos seleccionados por el usuario que recogeremos en el Controlador
	
	//Variables de la vista
	private JButton bBomba1, bBomba2, bTablero1, bTablero2;
	private JCheckBox cbAleatorio;
	private JButton bAceptar, bCancelar;
	
	/**
	 * Constructor
	 */
	public DialogoCambiarApariencia(){
		// Poner el dialogo modal, darle t�tulo y gestor de esquemas
		this.setModal(true);
		this.setTitle("Cambiar apariencia");
		this.setLayout(new BorderLayout(10,10));
		
		//A�adir los 3 paneles
		this.add("North",prepArriba());
		this.add("Center",prepCentro());
		this.add("South",prepSur());
		
		bomba = true;//por defecto
		tablero = true;//por defecto
		
		// A�adir el control
		bBomba1.addActionListener(this);
		bBomba2.addActionListener(this);
		bTablero1.addActionListener(this);
		bTablero2.addActionListener(this);
		bAceptar.addActionListener(this);
		bCancelar.addActionListener(this);
		
		// Control de cierre, pack, setVisible,...
		this.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
		this.setPreferredSize(new Dimension(277,220));
		this.pack();
		this.setVisible(true);
		this.setResizable(false);
	}
	/**
	 * M�todos de apoyo al constructor
	 */
	private JPanel prepArriba(){
		JPanel pArriba = new JPanel(new BorderLayout());
		pArriba.setBorder(new TitledBorder(new LineBorder(Color.GRAY, 2),"Seleccionar bomba"));
		iBomba1 = new ImageIcon("img/bomba1_32.png"); 
		bBomba1 = new JButton(iBomba1);										pArriba.add("West",bBomba1);
		bBomba1.setBorder(new LineBorder(Color.BLACK, 2));
		bBomba1.setBackground(Color.GRAY);
																			pArriba.add("Center",new JLabel());
		iBomba2 = new ImageIcon("img/bomba2_32.png");
		bBomba2 = new JButton(iBomba2);				pArriba.add("East",bBomba2);
		bBomba2.setBorder(new LineBorder(Color.BLACK, 1));
		bBomba2.setBackground(Color.WHITE);
		
		return pArriba; 
	}
	
	private JPanel prepCentro(){
		JPanel pCentro = new JPanel(new BorderLayout());
		pCentro.setBorder(new TitledBorder(new LineBorder(Color.GRAY, 2),"Seleccionar tablero"));
		bTablero1 = new JButton(new ImageIcon("img/tablero1_32.png"));										pCentro.add("West",bTablero1);
		bTablero1.setBorder(new LineBorder(Color.BLACK, 2));
		bTablero1.setBackground(Color.GRAY);
																									pCentro.add("Center",new JLabel());
		bTablero2 = new JButton(new ImageIcon("img/tablero2_32.png"));										pCentro.add("East",bTablero2);
		bTablero2.setBorder(new LineBorder(Color.BLACK, 1));
		bTablero2.setBackground(Color.WHITE);
		
		return pCentro; 
	}
	
	private JPanel prepSur(){
		JPanel pSur = new JPanel(new BorderLayout());
		
		cbAleatorio = new JCheckBox("Elegir bomba y tablero de forma aleatoria ");						pSur.add("North",cbAleatorio);
		
		
		JPanel pSurSur = new JPanel(new BorderLayout());												pSur.add("South",pSurSur);
		bAceptar = new JButton("Aceptar");																pSurSur.add("West",bAceptar);
		bAceptar.setPreferredSize(new Dimension(85,20));
		bCancelar = new JButton("Cancelar");															pSurSur.add("East",bCancelar);
		bCancelar.setPreferredSize(new Dimension(85,20));
		
		return pSur; 
	}
	
	
	
	/**
	 * ActionListener
	 */
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		Object ob = e.getSource();
		
		if(ob == bBomba1){
			bBomba1.setBorder(new LineBorder(Color.BLACK, 2));
			bBomba1.setBackground(Color.GRAY);
			bBomba2.setBorder(new LineBorder(Color.BLACK, 1));
			bBomba2.setBackground(Color.WHITE);
			bomba = true;
		}
		if(ob == bBomba2){
			bBomba1.setBorder(new LineBorder(Color.BLACK, 1));
			bBomba1.setBackground(Color.WHITE);
			bBomba2.setBorder(new LineBorder(Color.BLACK, 2));
			bBomba2.setBackground(Color.GRAY);
			bomba = false;
		}
		if(ob == bTablero1){
			bTablero1.setBorder(new LineBorder(Color.BLACK, 2));
			bTablero1.setBackground(Color.GRAY);
			bTablero2.setBorder(new LineBorder(Color.BLACK, 1));
			bTablero2.setBackground(Color.WHITE);
			tablero = true;
		}
		if(ob == bTablero2){
			bTablero1.setBorder(new LineBorder(Color.BLACK, 1));
			bTablero1.setBackground(Color.WHITE);
			bTablero2.setBorder(new LineBorder(Color.BLACK, 2));
			bTablero2.setBackground(Color.GRAY);
			tablero = false;
		}
		if(ob == bCancelar){
			this.setVisible(false);
		}
		if(ob == bAceptar){
			if(cbAleatorio.isSelected()){// Aleatorio
				Random ran = new Random();
				bomba=ran.nextBoolean();// Bomba aleatoria
				tablero=ran.nextBoolean();// Tablero aleatorio
			}
			
			if(bomba)
				iBombaFinal = new ImageIcon("img/bomba1_16.png");

			else
				iBombaFinal = new ImageIcon("img/bomba2_16.png");

			if(tablero)
				iTableroFinal = new ImageIcon("img/tablero1_18.png");

			else
				iTableroFinal = new ImageIcon("img/tablero2_18.png");

			this.setVisible(false);
		}
	}
	
	/**
	 * Get
	 */
	public boolean isBomba() {return bomba;}
	public boolean isTablero() {return tablero;}
	public Icon getiBomba1() {return iBomba1;}
	public Icon getiBomba2() {return iBomba2;}
	public Icon getiTablero1() {return iTablero1;}
	public Icon getiTablero2() {return iTablero2;}
	public Icon getiBombaFinal() {return iBombaFinal;}
	public Icon getiTableroFinal() {return iTableroFinal;}
	public JButton getbBomba1() {return bBomba1;}
	public JButton getbBomba2() {return bBomba2;}
	public JButton getbTablero1() {return bTablero1;}
	public JButton getbTablero2() {return bTablero2;}
	public JCheckBox getCbAleatorio() {return cbAleatorio;}
	public JButton getbAceptar() {return bAceptar;}
	public JButton getbCancelar() {return bCancelar;}
}
